<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<!DOCTYPE html>
<html>
<head>
  <meta name="referrer" content="never">
</head>
<body>
<script>
  /**
   * This file is responsible for restoring session history.
   * For now, that means we simply restore the last selected page in this tab.
   * We don't try to restore session history due to bug 1238006.
   */
  (function () {
      // Pull the session out of the history query argument. 
      // The session is a JSON-stringified array of all URLs to restore for this tab, plus the last active index.
      var dataIndex = document.location.href.search("history");
      var sessionRestoreComponents = JSON.parse(unescape(document.location.href.substring(dataIndex + "history=".length)));
      var urlList = sessionRestoreComponents['history'];
      var currentPage = sessionRestoreComponents['currentPage'];

      // Find the index of the last selected page and restore it.
      var restoreIndex = urlList.length + currentPage - 1;
      if (restoreIndex < 0 || restoreIndex >= urlList.length) {
          restoreIndex = urlList.length - 1;
      }

      window.location = urlList[restoreIndex];

      // Fire the restore event so that future URL changes are reflected in the UI.
      webkit.messageHandlers.sessionRestoreHelper.postMessage({ name: "didRestoreSession" });
  }) ();
</script>
</body>
</html>
